<template>
	<view class="uni-padding-wrap">
		
		<view style="width: 100%;height: 100px;background: #195B40;text-align: center;">
			<view style="font-size: 25px;color: white;">今日测量体温</view>
			<view style="font-size: 18px;color: white;">欢迎您,{{datelist.person_name}}</view>
		</view>
		<view class="uni-form-item uni-column" style="margin:0 20px;height: 40px;">
				<radio-group name="sex" @change="radioChange">
					<label>
						<radio value="staff"  :disabled='selectdisabled' checked="checked" /><text>员工</text>
						<radio value="Tourist"  :disabled='selectdisabled' /><text>访客</text>
					</label>
				</radio-group>
			</view>
		<view style="background: white;width: 80%;margin: 10px 10%;height: 40px;text-align: center;">
			<input v-model="datelist.temperature" maxlength="4" style="line-height: 40px;height: 40px;" type="digit"  placeholder="请输入测量体温" :disabled="postdisabled"/>
		</view>
		<view style="width: 80%;margin: 10px 10%;;text-align: center;">
			<picker :range="years" :disabled="pickerdisabled" @change="yearChange" style="line-height: 40px;background: white;" v-if="pickershow">
			                 {{ years[yearsIndex] }} 
			</picker>
			
			<input v-model="datelist.traffic" v-if="showtraffic" @keyup.native="inputChange($event)" @keydown.native="inputChange($event)" style="line-height: 40px;height: 40px;position: unset;margin-top: 10px;background: white;" type="text" value="" :disabled="postdisabled" placeholder="请输入交通工具"/>
		</view>
		<view v-if="visitorif">
			<view style="background: white;width: 80%;margin: 10px 10% 10px 10%;height: 40px;text-align: center;">
				<input v-model="datelist.visitor_for" style="line-height: 40px;height: 40px;" type="text" placeholder="请输入您要拜访的公司(员工)名" :disabled="postdisabled"/>
			</view>
			<view style="background: white;width: 80%;margin: 10px 10%;height: 40px;text-align: center;">
				<input v-model="datelist.visitor_for_mobile" style="line-height: 40px;height: 40px;" type="text" placeholder="请输入您要拜访的公司(员工)联系方式" :disabled="postdisabled"/>
			</view>
		</view>
		<view style="color:red;text-align: center;" v-if='successpost===true'>*已提交,请勿重复提交
			<p>今日温度:{{datelist.temperature}}</p>
		</view>
		<button style="background: #195B40;border-radius: 15px;" :disabled="postdisabledBtn"  type="primary" @click="addT">保存自测体温</button>
		<navigator  url="../index/index">
			<span style="backgroutruend: #195B40;border-radius: 25px;width: 247px;height: 50px;font-weight: 550;margin: 10px auto;color: red;">点击前往注册公司码>>></span>
		</navigator>
		<view style="text-align: center;margin: 15px auto;color: #666666;">
			<p>温馨提醒</p>
			<p>温度填写请在34.0-42.0之间</p>
			<p>疫情期间,感谢您的理解与配合!</p>
			<p>戴口罩\勤洗手\测体温\少聚集\多通风</p>
		</view>
		
	</view>
</template>

<style>
	page{
		height: unset;
	}
</style>
<script>
	import {addTemperature} from '../../api/index.js'
	
	export default {
		
		data() {
			return {
				successpost:false,
				disabled:false,
				postdisabled:false,
				postdisabledBtn:false,
				type:'',
				showtraffic:false,
				pickerdisabled:false,
				selectdisabled:false,
				visitorif:false,
				yearsIndex:'0',
				pickershow:true,
				years:["请选择交通方式",'公共交通','私家车','出租车','单车','其他'],
				datelist:{
					person_name:wx.getStorageSync("person_name"),
					person_sys_id:wx.getStorageSync("personid"),
					company_id:'',
					traffic:'',
					temperature:'',
					visitor_for:'',
					visitor_for_mobile:''
				},
			}
		},
		onLoad: function(option) {
			// this.datelist.person_name =wx.getStorageSync("person_name")
			if(option.id) {
				this.datelist.person_sys_id = option.id;
				this.disabled=true;
			}
		},
		methods: {
			yearChange:function(e){
			                 console.log(e)
							 console.log(e.detail.value)
							 this.yearsIndex = e.detail.value;
			                 //获得对象的 detail的 value
			                 //通过数组的下标改变显示在页面的值
							 if(e.detail.value == 5){
								 this.showtraffic = true
							 }else{
			                 
							 }
			             },
			// 选择是游客还是员工
			radioChange: function(evt) {
					console.log(evt.detail.value)
					// 选择为游客时
					if(evt.detail.value == 'Tourist'){
						this.visitorif = true
					}else{
						this.visitorif = false
					}
				},
			inputChange(e) {
			      const o = e.target;
			      o.value = o.value.replace(/[^\u4E00-\u9FA5]/g, ''); // 清除除了中文以外的输入的字符
			　　　this.name = o.value;
			    },
			addT(){
				// 如果不是游客
				console.log(this.visitorif)
				var that =this
				if(that.visitorif == true){
					console.log('是游客')
					that.datelist.company_id=wx.getStorageSync("companyId")
					if(this.datelist.temperature>42||this.datelist.temperature<34){
						uni.showToast({
						    title: '温度请输入34.0-42.0',
							icon:'none'
						});
					}else if(that.yearsIndex =='0'){
						uni.showToast({
						    title: '请选择交通工具',
							icon:'none'
						});
					}else if(that.datelist.visitor_for.length ===0){
						uni.showToast({
						    title: '请输入拜访对象',
							icon:'none'
						});
					}else if(that.datelist.visitor_for_mobile.length !=11){
						uni.showToast({
						    title: '请正确输入联系方式',
							icon:'none'
						});
					}else{
						if(that.yearsIndex !='5'){
							that.datelist.traffic = that.years[that.yearsIndex]
						}
						if(that.datelist.traffic ==''){
							uni.showToast({
							    title: '请输入交通工具',
								icon:'none'
							});
						}else{
							var retData3=addTemperature(that.datelist)
							Promise.resolve(retData3).then(function (result) {
								if(result.code=='0'){
									that.postdisabled =true
									that.postdisabledBtn =true
									that.successpost =true
									that.selectdisabled=true
									that.pickerdisabled=true
									console.log(result)
									uni.showModal({
										content: `提交成功,`,
										showCancel: false,
									})
								}
							})
						}
						
					}
				}else{
					that.datelist.company_id='';
					if(that.datelist.temperature>42||that.datelist.temperature<34){
						uni.showToast({
						    title: '温度请输入34.0-42.0',
							icon:'none'
						});
					}else if(that.yearsIndex =='0'){
						uni.showToast({
						    title: '请选择交通工具',
							icon:'none'
						});
					}else{
						if(that.yearsIndex !='5'){
							that.datelist.traffic = that.years[that.yearsIndex]
						}
						if(that.datelist.traffic ==''){
							uni.showToast({
							    title: '请输入交通工具',
								icon:'none'
							});
						}else{
						var retData3=addTemperature(that.datelist)
						Promise.resolve(retData3).then(function (result) {
							if(result.code=='0'){
								that.postdisabled =true
								that.postdisabledBtn =true
								that.successpost =true
								that.pickerdisabled=true
								that.selectdisabled=true
								console.log(result)
								uni.showModal({
									content: `提交成功,`,
									showCancel: false,
								})
							}
						})
						}
					}
				}
				
			}
		},
		watch:{
			'yearsIndex'(newVal){
				if(newVal =='5'){
					this.pickerdisabled=true
					console.log('123')
					this.pickershow = false
						this.datelist.traffic =''
				}else{
					this.pickershow = true
					this.pickerdisabled=false
				}
			}
		},
		mounted() {
			if(wx.getStorageSync("personid")){
				this.disabled = true;
				this.datelist.person_sys_id =wx.getStorageSync("personid");
			}
			
		}
	}
</script>
